home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 March / SGI IRIX Patches 1995 Mar.iso / 6.0.1_patches / patchSG0000283 / patchSG0000283.idb / usr / include / rpc / xdr.h.z / xdr.h
C/C++ Source or Header  |  1995-03-10  |  12KB  |  375 lines

  1. #ifndef __RPC_XDR_H__
  2. #define __RPC_XDR_H__
  3. #ident "$Revision: 2.24 $"
  4. /*
  5.  *
  6.  * Copyright 1992, Silicon Graphics, Inc.
  7.  * All Rights Reserved.
  8.  *
  9.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  10.  * the contents of this file may not be disclosed to third parties, copied or
  11.  * duplicated in any form, in whole or in part, without the prior written
  12.  * permission of Silicon Graphics, Inc.
  13.  *
  14.  * RESTRICTED RIGHTS LEGEND:
  15.  * Use, duplication or disclosure by the Government is subject to restrictions
  16.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  17.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  18.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  19.  * rights reserved under the Copyright Laws of the United States.
  20.  */
  21.  
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25.  
  26. /*    @(#)xdr.h    1.2 90/07/19 4.1NFSSRC SMI    */
  27.  
  28. /* 
  29.  * Copyright (c) 1990 by Sun Microsystems, Inc.
  30.  *      @(#)xdr.h 1.22 88/02/08 SMI 
  31.  */
  32.  
  33. /*
  34.  * xdr.h, External Data Representation Serialization Routines.
  35.  */
  36.  
  37.  
  38. /*
  39.  * XDR provides a conventional way for converting between C data
  40.  * types and an external bit-string representation.  Library supplied
  41.  * routines provide for the conversion on built-in C data types.  These
  42.  * routines and utility routines defined here are used to help implement
  43.  * a type encode/decode routine for each user-defined type.
  44.  *
  45.  * Each data type provides a single procedure which takes two arguments:
  46.  *
  47.  *    bool_t
  48.  *    xdrproc(xdrs, argresp)
  49.  *        XDR *xdrs;
  50.  *        <type> *argresp;
  51.  *
  52.  * xdrs is an instance of a XDR handle, to which or from which the data
  53.  * type is to be converted.  argresp is a pointer to the structure to be
  54.  * converted.  The XDR handle contains an operation field which indicates
  55.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  56.  *
  57.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  58.  * data can be freed with the XDR_FREE operation.
  59.  *
  60.  * We write only one procedure per data type to make it easy
  61.  * to keep the encode and decode procedures for a data type consistent.
  62.  * In many cases the same code performs all operations on a user defined type,
  63.  * because all the hard work is done in the component type routines.
  64.  * decode as a series of calls on the nested data types.
  65.  */
  66.  
  67. /*
  68.  * NOTE that for 64 bit software, the xdr representation of a long
  69.  * and a u_long, is 32 bits. The reasons for this:
  70.  *    All existing protocols expect only 32-bit data. Obviously
  71.  *    we cannot change what is going into rpc packets.
  72.  *
  73.  *    All existing code makes liberal use of xdr_long, XDR_PUTLONG, etc.
  74.  *    To change the behavior of xdr_long to put out 64 bits would require
  75.  *    that all code that is to be compiled for 64 bits would have to be
  76.  *    changed to use xdr_int, XDR_PUTINT, etc.
  77.  *
  78.  *    Moreover, all the data types being used as arguments to those
  79.  *    xdr calls would have to be changed from long/u_long to int/u_int.
  80.  *    That is not desirable, and in many cases would violate our 64bit
  81.  *    abi, or supported API's.
  82.  *
  83.  *    Any 64 bit xdr user that wants to interoperate with 32 bit programs
  84.  *    will be able to without changing any current code.
  85.  */
  86.  
  87. /*
  88.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  89.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  90.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  91.  * request.
  92.  */
  93. enum xdr_op {
  94.     XDR_ENCODE=0,
  95.     XDR_DECODE=1,
  96.     XDR_FREE=2
  97. };
  98.  
  99. /*
  100.  * This is the number of bytes per unit of external data.
  101.  */
  102. #define BYTES_PER_XDR_UNIT    (4)
  103. #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  104.             * BYTES_PER_XDR_UNIT)
  105.  
  106. #if (_MIPS_SZLONG == 32)
  107. typedef    long    xdr_long_t;
  108. typedef    u_long    xdr_u_long_t;
  109. #endif
  110. #if (_MIPS_SZLONG == 64)
  111. #include <sys/types.h>
  112. typedef    __int32_t    xdr_long_t;
  113. typedef    __uint32_t    xdr_u_long_t;
  114. #endif
  115.  
  116. struct __xdr_s;
  117. struct xdr_ops {
  118.     /* get an xdr_long from underlying stream */
  119.     bool_t    (*x_getlong)(struct __xdr_s *, long *);
  120.  
  121.     /* put an xdr_long to the stream */
  122.     bool_t    (*x_putlong)(struct __xdr_s *, long *);
  123.  
  124. #if (_MIPS_SZLONG != _MIPS_SZINT)
  125.     /* get an xdr_int from underlying stream */
  126.     bool_t    (*x_getint)(struct __xdr_s *, int *);
  127.  
  128.     /* put an xdr_int to the stream */
  129.     bool_t    (*x_putint)(struct __xdr_s *, int *);
  130. #endif
  131.  
  132.     /* get some bytes from the stream */
  133.     bool_t    (*x_getbytes)(struct __xdr_s *, void *, u_int);
  134.  
  135.     /* put some bytes to " */
  136.     bool_t    (*x_putbytes)(struct __xdr_s *, void *, u_int);
  137.  
  138.     /* returns bytes off from beginning */
  139.     u_int    (*x_getpostn)(struct __xdr_s *);
  140.  
  141.     /* lets you reposition the stream */
  142.     bool_t    (*x_setpostn)(struct __xdr_s *, u_int);
  143.  
  144.     /* buf quick ptr to buffered data */
  145.     long *    (*x_inline)(struct __xdr_s *, int);
  146.  
  147.     /* free privates of this xdr_stream */
  148.     void    (*x_destroy)(struct __xdr_s *);
  149. };
  150.  
  151. /*
  152.  * The XDR handle.
  153.  * Contains operation which is being applied to the stream,
  154.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  155.  * and two private fields for the use of the particular impelementation.
  156.  */
  157. typedef struct __xdr_s {
  158.     enum xdr_op    x_op;        /* operation; fast additional param */
  159.     struct xdr_ops  *x_ops;
  160.     caddr_t     x_public;    /* users' data */
  161.     caddr_t    x_private;    /* pointer to private data */
  162.     caddr_t      x_base;        /* private used for position info */
  163.     int        x_handy;    /* extra private word */
  164. } XDR;
  165.  
  166. /*
  167.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  168.  *
  169.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  170.  * The opaque pointer generally points to a structure of the data type to be
  171.  * decoded.  If the opaque pointer is NULL (0), then the type routines should
  172.  * allocate dynamic storage of the appropriate size and return it.
  173.  */
  174. typedef    bool_t (*xdrproc_t)();
  175.  
  176. /*
  177.  * Operations defined on a XDR handle
  178.  *
  179.  * XDR        *xdrs;
  180.  * xdr_long_t    *longp;
  181.  * void     *addr;
  182.  * u_int     len;
  183.  * u_int     pos;
  184.  */
  185. #define XDR_GETLONG(xdrs, longp)            \
  186.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  187. #define xdr_getlong(xdrs, longp)            \
  188.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  189.  
  190. #define XDR_PUTLONG(xdrs, longp)            \
  191.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  192. #define xdr_putlong(xdrs, longp)            \
  193.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  194.  
  195. #if (_MIPS_SZLONG != _MIPS_SZINT)
  196.  
  197. #define XDR_GETINT(xdrs, intp)            \
  198.     (*(xdrs)->x_ops->x_getint)(xdrs, intp)
  199. #define xdr_getint(xdrs, intp)            \
  200.     (*(xdrs)->x_ops->x_getint)(xdrs, intp)
  201.  
  202. #define XDR_PUTINT(xdrs, intp)            \
  203.     (*(xdrs)->x_ops->x_putint)(xdrs, intp)
  204. #define xdr_putint(xdrs, intp)            \
  205.     (*(xdrs)->x_ops->x_putint)(xdrs, intp)
  206.  
  207. #endif    /* _MIPS_SZLONG != _MIPS_SZINT */
  208.  
  209. #define XDR_GETBYTES(xdrs, addr, len)            \
  210.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  211. #define xdr_getbytes(xdrs, addr, len)            \
  212.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  213.  
  214. #define XDR_PUTBYTES(xdrs, addr, len)            \
  215.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  216. #define xdr_putbytes(xdrs, addr, len)            \
  217.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  218.  
  219. #define XDR_GETPOS(xdrs)                \
  220.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  221. #define xdr_getpos(xdrs)                \
  222.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  223.  
  224. #define XDR_SETPOS(xdrs, pos)                \
  225.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  226. #define xdr_setpos(xdrs, pos)                \
  227.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  228.  
  229. #define    XDR_INLINE(xdrs, len)                \
  230.     (xdr_long_t *)(*(xdrs)->x_ops->x_inline)(xdrs, len)
  231. #define    xdr_inline(xdrs, len)                \
  232.     (xdr_long_t *)(*(xdrs)->x_ops->x_inline)(xdrs, len)
  233.  
  234. #define    XDR_DESTROY(xdrs)                \
  235.     (*(xdrs)->x_ops->x_destroy)(xdrs)
  236. #define    xdr_destroy(xdrs) XDR_DESTROY(xdrs)
  237.  
  238. /*
  239.  * Support struct for discriminated unions.
  240.  * You create an array of xdrdiscrim structures, terminated with
  241.  * a entry with a null procedure pointer.  The xdr_union routine gets
  242.  * the discriminant value and then searches the array of structures
  243.  * for a matching value.  If a match is found the associated xdr routine
  244.  * is called to handle that part of the union.  If there is
  245.  * no match, then a default routine may be called.
  246.  * If there is no match and no default routine it is an error.
  247.  */
  248. #define NULL_xdrproc_t ((xdrproc_t)0)
  249. struct xdr_discrim {
  250.     int    value;
  251.     xdrproc_t proc;
  252. };
  253.  
  254. /*
  255.  * In-line routines for fast encode/decode of primitve data types.
  256.  * Caveat emptor: these use single memory cycles to get the
  257.  * data from the underlying buffer, and will fail to operate
  258.  * properly if the data is not aligned.  The standard way to use these
  259.  * is to say:
  260.  *    if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  261.  *        return (FALSE);
  262.  *    <<< macro calls >>>
  263.  * where ``count'' is the number of bytes of data occupied
  264.  * by the primitive data types.
  265.  *
  266.  * N.B. and frozen for all time: each data type here uses 4 bytes
  267.  * of external representation.
  268.  */
  269. #define IXDR_GET_LONG(buf)    \
  270.         ((long)ntohl((u_long)*(xdr_long_t *)(buf)++))
  271. #define IXDR_PUT_LONG(buf, v)    \
  272.         (*(xdr_long_t *)(buf)++ = (xdr_long_t)htonl((u_long)v))
  273.  
  274. #define IXDR_GET_BOOL(buf)        ((bool_t)IXDR_GET_LONG(buf))
  275. #define IXDR_GET_ENUM(buf, t)        ((t)IXDR_GET_LONG(buf))
  276. #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_LONG(buf))
  277. #define IXDR_GET_SHORT(buf)        ((short)IXDR_GET_LONG(buf))
  278. #define IXDR_GET_U_SHORT(buf)        ((u_short)IXDR_GET_LONG(buf))
  279.  
  280. #define IXDR_PUT_BOOL(buf, v)        IXDR_PUT_LONG((buf), ((xdr_long_t)(v)))
  281. #define IXDR_PUT_ENUM(buf, v)        IXDR_PUT_LONG((buf), ((xdr_long_t)(v)))
  282. #define IXDR_PUT_U_LONG(buf, v)        IXDR_PUT_LONG((buf), ((xdr_long_t)(v)))
  283. #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((xdr_long_t)(v)))
  284. #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_LONG((buf), ((xdr_long_t)(v)))
  285.  
  286. /*
  287.  * These are the "generic" xdr routines.
  288.  */
  289. extern void    xdr_free(xdrproc_t, void *);
  290. extern bool_t    xdr_void(XDR *, void *);
  291. extern bool_t    xdr_int(XDR *, int *);
  292. extern bool_t    xdr_u_int(XDR *, u_int *);
  293. extern bool_t    xdr_long(XDR *, long *);
  294. extern bool_t    xdr_u_long(XDR *, u_long *);
  295. extern bool_t    xdr_short(XDR *, short *);
  296. extern bool_t    xdr_u_short(XDR *, u_short *);
  297. extern bool_t    xdr_char(XDR *, char *);
  298. extern bool_t    xdr_u_char(XDR *, u_char *);
  299. extern bool_t    xdr_bool(XDR *, bool_t *);
  300. extern bool_t    xdr_enum(XDR *, enum_t *);
  301. extern bool_t    xdr_array(XDR *, caddr_t *, u_int *, u_int, u_int, xdrproc_t);
  302. extern bool_t    xdr_bytes(XDR *, char **, u_int *, u_int);
  303. extern bool_t    xdr_opaque(XDR *, void *, u_int);
  304. extern bool_t    xdr_string(XDR *, char **, u_int);
  305. extern bool_t    xdr_union(XDR *, enum_t *, void *, struct xdr_discrim *, 
  306.             xdrproc_t);
  307. extern bool_t    xdr_reference(XDR *, caddr_t *, u_int, xdrproc_t);
  308. extern bool_t    xdr_time_t(XDR *, time_t *);
  309. #ifndef _KERNEL
  310. extern bool_t    xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
  311. extern bool_t    xdr_float(XDR *, float *);
  312. extern bool_t    xdr_double(XDR *, double *);
  313. extern bool_t    xdr_pointer(XDR *, caddr_t *, u_int, xdrproc_t);
  314. extern bool_t    xdr_wrapstring(XDR *, char **);
  315. #endif /* !_KERNEL */
  316.  
  317. /*
  318.  * Common opaque bytes objects used by many rpc protocols;
  319.  * declared here due to commonality.
  320.  */
  321. #define MAX_NETOBJ_SZ 1024 
  322. struct netobj {
  323.     u_int    n_len;
  324.     char    *n_bytes;
  325. };
  326. typedef struct netobj netobj;
  327. extern bool_t   xdr_netobj(XDR *, netobj *);
  328.  
  329. extern int xdr_authkern(XDR *);
  330.  
  331. /*
  332.  * These are the public routines for the various implementations of
  333.  * xdr streams.
  334.  */
  335. /* XDR using memory buffers */
  336. extern void   xdrmem_create(XDR *, void *, u_int, enum xdr_op);    
  337.  
  338. #ifndef _KERNEL 
  339. #include <stdio.h>
  340. extern void   xdrstdio_create(XDR *, FILE *, enum xdr_op);
  341.  
  342. /* XDR pseudo records for TCP */
  343. extern void   xdrrec_create(XDR *, u_int, u_int, void *, 
  344.                 int (*)(void *, void *, u_int), 
  345.                 int (*)(void *, void *, u_int));
  346. /* Make end of xdr record */
  347. extern bool_t xdrrec_endofrecord(XDR *, bool_t);    
  348.  
  349. /* Move to beginning of next record */
  350. extern bool_t xdrrec_skiprecord(XDR *);    
  351.  
  352. /* True if no more input */
  353. extern bool_t xdrrec_eof(XDR *);    
  354.  
  355. /* 
  356.  * Returns up to the number of bytes requested,
  357.  * 0 indicates end-of-record, -1 means something very bad happened.
  358.  */
  359. extern int xdrrec_readbytes(XDR *, caddr_t, u_int);
  360.  
  361. #else
  362. /* XDR using kernel mbufs */
  363. struct mbuf;
  364. extern void xdrmbuf_init(XDR *, struct mbuf *, enum xdr_op);
  365. extern bool_t   xdrmbuf_getmbuf(XDR *, struct mbuf **, u_int *);
  366. extern bool_t   xdrmbuf_putbuf(XDR *, caddr_t, u_long, int (*dfunc)(),
  367.             long, int (*ffunc)(), long);
  368. #endif /* !_KERNEL */
  369.  
  370. #ifdef __cplusplus
  371. }
  372. #endif
  373.  
  374. #endif /* !__RPC_XDR_H__ */
  375.